//
//  TriangleMesh.hpp
//  RayTracer
//
//  Created by 俞云康 on 3/25/16.
//  Copyright © 2016 yushroom. All rights reserved.
//

#ifndef TriangleMesh_hpp
#define TriangleMesh_hpp

#include "Shape.hpp"

class TriangleMesh;

class Triangle : public Shape
{
public:
    uint32_t idx[3];
    const TriangleMesh* mesh;
    
    Triangle(const std::shared_ptr<Transform>& transform);
    //Triangle(uint32_t idx1, uint32_t idx2, uint32_t idx3);
    
    virtual bool intersect(const Ray& r, HitInfo* hit) const override;
    virtual bool intersect(const Ray& r) const override;
    
	virtual float area() const override;
	virtual Position sample(const Sample& sample, Normal* shape_normal) const override;

    void getUVs(float2 uv[3]) const;
};

class TriangleMesh{
public:
    string name;
    std::shared_ptr<Transform> transform;
    //shared_ptr<Material> material;
    int n_triangles;
    int n_vertices;
    vector<unsigned int> indices;
    vector<Position> positions;
    vector<Normal> normals;
    vector<float3> tangents;
    vector<float2> uvs;
    
    std::vector<shared_ptr<Triangle>> triangles;
    
    TriangleMesh(const std::string& name, const std::shared_ptr<Transform>& transform, const string& path);
    
    ~TriangleMesh() {
        //info("~TriangleMesh\n");
    }
    
    static shared_ptr<TriangleMesh> create(const Params& params);
    
//    virtual bool intersect(const Ray& r, HitInfo* hit) const override;
//    
//    virtual bool intersect(const Ray& r) const override;
    
private:
    void loadFromObj(const string& path);
};


#endif /* TriangleMesh_hpp */
